package kmi.droid.particles.sample;

import kmi.droid.animatron.ParticleSystem;
import kmi.droid.animatron.Vector2;
import kmi.square.wallpaper.KmiSquareWallpaperPainting;
import android.graphics.Color;

public class SquareParticleSystem extends ParticleSystem<SquareParticle>
{
	// / For reference to touched point;
	private KmiSquareWallpaperPainting painting;

	public SquareParticleSystem(KmiSquareWallpaperPainting painting)
	{
		this.painting = painting;
		SquareParticle square1 = new SquareParticle(new Vector2(100, 100),
				Color.RED, 4f);
		SquareParticle square2 = new SquareParticle(new Vector2(100, 100),
				Color.GREEN, 5f);
		SquareParticle square3 = new SquareParticle(new Vector2(100, 100),
				Color.YELLOW, 6f);

		particles.add(square1);
		particles.add(square2);
		particles.add(square3);

	}

	@Override
	public boolean getIsAlive()
	{
		return true;
	}

	@Override
	public void update(float dt)
	{
		for (int i = 0; i < particles.size(); i++)
		{
			SquareParticle sp = particles.get(i);
			sp.invalidate(painting.getTouchPoint());
			float x = sp.getPosition().X;
			float y = sp.getPosition().Y;

			if (x > painting.getWidth() || x < 0)
			{
				sp.Acceleration = Vector2.zero();
				sp.Velocity.X = -sp.Velocity.X;
				if (x < 0)
					sp.setPosition(new Vector2(0, sp.getPosition().Y));
				else
					sp.setPosition(new Vector2(painting.getWidth(), sp
							.getPosition().Y));
			}

			if (y > painting.getHeight() || y < 0)
			{
				sp.Acceleration = Vector2.zero();
				sp.Velocity.Y = -sp.Velocity.Y;
				if (y < 0)
					sp.setPosition(new Vector2(sp.getPosition().X, 0));
				else
					sp.setPosition(new Vector2(sp.getPosition().X, painting
							.getHeight()));
			}
		}

		super.update(dt);
	}
}